import urlConfig from './config.js'

import qs from 'qs'

let ApiUrl = urlConfig

export default {
	apiUrl:ApiUrl,
	get(url, needToken ,options) {
		let newUrl = url
		if (url.indexOf('http') == -1){
			newUrl = ApiUrl + url;
		}
		var token = uni.getStorageSync('hqt-mix-token')
		let aData = {
			...options
		}
		return new Promise((resolve, reject) => {
			uni.request({
				url: newUrl,
				data: aData,
				dataType: 'json',
				header:{
					"Authorization":token				
				},
				success(res) {
					resolve(res);
				},
				fail(error) {
					console.log('错误1');
					reject(error);
				}
			})
		});

	},

	post(url, needToken ,options) {
		let newUrl = ApiUrl + url;
		var token = uni.getStorageSync('hqt-mix-token')
		let aData = {
			...options
		}
		if (needToken){
			aData.utoken = token
		}
		return new Promise((resolve, reject) => {
			uni.request({
				method: "POST",
				url: newUrl,
				data: aData,
				dataType: 'json',
				header: {
					"content-type": "application/x-www-form-urlencoded",
				},
				success(res) {
					resolve(res);

				},
				fail(error) {
					reject(error);
				}
			})
		});
	},
	
	postJson(url, options) {
		let newUrl = ApiUrl + url;
		var token = uni.getStorageSync('hqt-mix-token')
		let aData = {
			...options
		}
		return new Promise((resolve, reject) => {
			uni.request({
				method: "POST",
				url: newUrl,
				data: aData,
				dataType: 'json',
				header: {
					"Authorization":token,
					"content-type": "application/json"
					
				},
				success(res) {
					resolve(res);
				},
				fail(error) {
					reject(error);
				}
			})
		});
	},
	
	toast: function(title, duration=1500) {
		if(title.length > 10){
			duration = 3000
		}
		if(title.length > 15){
			duration = 3500
		}
		uni.showToast({
			title,
			duration,
			icon: 'none'
		});
	}
	
}


